# -*- coding: utf-8 -*-
import logging
import os
import random
import sys
import time
import traceback
from datetime import timedelta

import requests
from bs4 import BeautifulSoup
from dateutil import parser
from lxml import etree

# add up one level dir into sys path
from common.lottery.handler import crawl_from_opencai

sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'base.settings'

from common.lottery.cyclical.gd_11x5.db.activity import fullfill_result
from common.lottery import LOTTERY_TYPE, PROXY_URL, OPENCAI_LOTTERY_TYPES
from common.timer import TIMER_EVENT_TYPE, TimerEvent
from common.utils import tz

URL_163 = 'http://caipiao.163.com/award/gd11xuan5/%s.html'

URL_500 = 'http://kaijiang.500.com/static/info/kaijiang/xml/gdsyxw/%s.xml'


def _parse_and_fill_163(resp, refer):
    soup = BeautifulSoup(resp, "lxml")
    tds = soup.find_all('td', {'class': 'start'})
    for td in tds:
        if td.get('data-award'):
            phase = '20' + td.get('data-period')
            number = td.get('data-award').split(' ')
            number = ','.join([str(int(n)) for n in number])
            activity = fullfill_result(phase, number, refer)
            if not activity:
                continue
            TimerEvent.submit(TIMER_EVENT_TYPE.CALC_STATS, {
                'number': number, 'term': phase,
                'activity_type': LOTTERY_TYPE.GD_11X5}, tz.now_ts())
            TimerEvent.submit(TIMER_EVENT_TYPE.ACTIVITY_ANNOUNCE, {
                'number': number, 'term': phase,
                'activity_type': LOTTERY_TYPE.GD_11X5}, tz.now_ts())


def _parse_and_fill_500(resp, refer):
    nodes = etree.fromstring(resp)
    for node in nodes:
        attrib = node.attrib
        phase = '20' + attrib['expect'].replace('-', '')
        number = ','.join(str(int(each)) for each in attrib['opencode'].split(','))
        activity = fullfill_result(phase, number, refer)
        if not activity:
            continue
        TimerEvent.submit(TIMER_EVENT_TYPE.CALC_STATS, {
            'number': number, 'term': phase,
            'activity_type': LOTTERY_TYPE.GD_11X5}, tz.now_ts())
        TimerEvent.submit(TIMER_EVENT_TYPE.ACTIVITY_ANNOUNCE, {
            'number': number, 'term': phase,
            'activity_type': LOTTERY_TYPE.GD_11X5}, tz.now_ts())


def crawl_history_from_163(date):
    url = URL_163 % date.strftime('%Y%m%d')
    try:
        response = requests.get(url, proxies={'http': PROXY_URL}, timeout=10)
    except Exception:
        raise
    if response.status_code != 200:
        raise RuntimeError('request error')
    _parse_and_fill_163(response.content, url)


def crawl_history_from_500(date):
    url = URL_500 % date.strftime('%Y%m%d')
    try:
        response = requests.get(url, proxies={'http': PROXY_URL}, timeout=10)
    except Exception:
        raise
    if response.status_code != 200:
        raise RuntimeError('request error')
    _parse_and_fill_500(response.content, url)


''' 在cron里面每天北京时间凌晨1点跑头一天
'''


def crawl_newest_from_163(now=None):
    '''官网爬虫
    '''
    now = now or tz.local_now()
    refer = URL_163 % now.strftime('%Y%m%d')
    url = refer
    try:
        response = requests.get(url, proxies={'http': PROXY_URL}, timeout=5)
    except Exception:
        return now
    if response.status_code != 200:
        return now
    _parse_and_fill_163(response.content, refer)
    return None


def crawl_newest_from_500(now=None):
    now = now or tz.local_now()
    refer = URL_500 % now.strftime('%Y%m%d')
    url = refer
    try:
        response = requests.get(url, proxies={'http': PROXY_URL}, timeout=5)
    except Exception:
        return now
    if response.status_code != 200:
        return now
    _parse_and_fill_500(response.content, refer)
    return now


def main(d):
    while True:
        try:
            crawl_from_opencai(LOTTERY_TYPE.GD_11X5)
        except Exception as e:
            logging.exception(u'%s error: %s' % (OPENCAI_LOTTERY_TYPES.get(LOTTERY_TYPE.GD_11X5), e))
        if d:
            break
        time.sleep(60)


if __name__ == '__main__':
    date = sys.argv[1] if len(sys.argv) > 1 else None
    main(date)
